﻿
#include "SchLeg.h"

#include "ICPRule.h"

int SchLeg::_countId = 0;
SchLeg::SchLeg(LEGTYPE legType, string flightId, string flightNo, BasAirport* dep, BasAirport* arr, time_t std,
               time_t sta, time_t date, string acType, string acTypeCrew, string acReg, D_OR_I dori, P_OR_C porc,
               time_t flyTime, REST_LEVEL restLevel, string crewOwner)
    : _legType(legType), _flightId(flightId), _flightNo(flightNo), _dep(dep), _arr(arr), _std(std), _sta(sta),
      _date(date), _acType(acType), _acTypeCrew(acTypeCrew), _acReg(acReg), _dori(dori), _porc(porc), _flyTime(flyTime),
      _restLevel(restLevel), _crewOwner(crewOwner) {
    _id = _countId++;

    _isTarget = false;
    _fixedLP = false;
    _baseList = {};
    _isBestAirline = false;
    _isZWWW = false;
    _flightFlag = "A";
    _flightType = "A";
    _costCover = 0;
    _dualCover = 0;

    string flightFlag = "A";
    if (this->getLegType() == LEG_BACKUP) {
        flightFlag = "B";
    } else if (this->getLegType() == LEG_VA) {
        flightFlag = "D";
    } else if (this->getLegType() == LEG_VB) {
        flightFlag = "D";
    }
    if (_flightId.find("PREV") == string::npos) {
        _voyage = flightFlag + this->getFlightNo() + this->getDep()->getCode3() + this->getArr()->getCode3();
    } else {
        _voyage = this->getFlightNo();
    }
}

SchLeg::~SchLeg() {}

void SchLeg::resetCover() {
    _costCover = 0;
    _dualCover = 0;
    if (isTarget()) {
        if (_dori == D_OR_I::D) {
            _costCover = ICPUtility::costCover;
            _dualCover = ICPUtility::costCover;
        } else {
            _costCover = 10 * ICPUtility::costCover;
            _dualCover = 10 * ICPUtility::costCover;
        }
    }
}

void SchLeg::print() {
    cout << "leg_" << _id;
    cout << " legType-" << _legType;
    cout << " flightId_" << _flightId;
    cout << " flightNo_" << _flightNo;
    cout << " dep-" << _dep->getCode3();
    cout << " arr-" << _arr->getCode3();
    cout << " std-" << getTimeStr(_std);
    cout << " sta-" << getTimeStr(_sta);
    cout << " date-" << getDateStr(_date);
    cout << " flyTime-" << _flyTime / 60 << "min";
    cout << " dori-" << _dori;
    cout << " dualCover:" << _dualCover;
    cout << endl;
}

bool SchLeg::cmpByTime(SchLeg* a, SchLeg* b) {
    if (a->getStd() < b->getStd()) {
        return true;
    } else if (a->getStd() == b->getStd() && a->getSta() < b->getSta()) {
        return true;
    }
    return false;
}